/*******************************************************************************
 * Copyright (c) 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
	var jsUrls=new Array();
	jsUrls[0]=__ctxPath+"/dwr/engine.js";
	jsUrls[1]=__ctxPath+"/dwr/util.js";
	jsUrls[2]=__ctxPath+"/dwr/interface/designManager.js";
	$importJs(jsUrls,function(){
	});

var designSelector={
 	
	selectMode:0,//选择模式,0=单选,1=多选  默认为单选
	
	title:null,
	designId:null,
	pageNum:1,//当前页
	
	textId:null,//callback function返回值ID
	
	textValue:null,//callback function返回值名称
	
	showDlg:function (textId,textValue){
		this.textId=textId;		
		this.textValue=textValue;
		this.userDlg=createOverlayDlg("designSelector",{
			title:'原创设计选择',
			width:720,
			height:500,
			buttons:{
				"确 定":function (){	
			var idsText="";
			var namesText="";
			var designId=$("input:radio[name='designId']");
			var title=$("input:hidden[name='title']");
			for(var i=0;i<designId.size();i++){	
				if(designId[i].checked){	
					idsText=$("input:radio[name='designId']").get(i).value;			
					namesText=$("input:hidden[name='title']").get(i).value;
					break;
					}
			}

			$("#"+textValue).attr("value",namesText);
			$("#"+textId).attr("value",idsText);
		
			$(this).dialog("close");
			},
				"取 消":function (){$(this).dialog("close");}
			},
			url:__ctxPath+"/admin/common/userDesigns.do?decorator=blank&cmd=typeIsEnabled"
		});
	},

	getSearchDesign:function(pageNum){
		this.pageNum=pageNum;	
	 	DWREngine.setAsync(false);
	 	designManager.getAllByTitle(this.title,pageNum,fillDesignFunction);
	},
	
	searchAction:function(){
		
		designSelector.title=$("#title").val();
		$('#_current_page_num').val(1);
		designSelector.getSearchDesign(1);
	}
}
function fillDesignFunction(designList){
	if (designList==null || designList.length==0) {
		if(designSelector.pageNum>1){
				$('#_current_page_num').val(0);
				alert('已是最后一页');
				return ;
			}else{

		 DWRUtil.removeAllRows("_design_data_body"); 
		 var tr =document.createElement("tr");
		 var td=document.createElement("td");
	
		 td.innerHTML="<font color='red'>没有任何数据</font>";
		 tr.appendChild(td);
	
	$("#_design_data_body").append(tr);
	$("#showname").hide();
	$("#page").hide();
	}
	} else { 
		DWRUtil.removeAllRows("_design_data_body");
		$("#showname").show();
		$("#page").show();
		DWRUtil.addRows("_design_data_body", designList, cellDesignFunctions); 
	}
	
}

function _designNext(){
	var designId=$("input:radio[name='designId']")
	if(designId.size()<10){
		alert('已经是最后一页');
		return;//尾页	
		}
	designSelector.getSearchDesign(parseInt($('#_current_page_num').val())+1);
	designSelector.pageNum=$('#_current_page_num').val()+1;
	$('#_current_page_num').attr("value",parseInt($('#_current_page_num').val())+1);
}




function _designPrevious(){

	if(parseInt($("#_current_page_num").val())<=1){//首页
		$("_current_page_num").value=1;
		alert('首页');
		return;
	}
	designSelector.getSearchDesign(parseInt($('#_current_page_num').val())-1);
	$('#_current_page_num').attr("value",parseInt($('#_current_page_num').val())-1);
}

//fill the table cell
var cellDesignFunctions = [
					 function(design) {
							var selectType="";
							if(this.selectMode=0){
								selectType="radio";
							}
							var str="<input type='radio' name='designId' value='"+design.designId+"' />"
							var span=document.createElement("span");
							span.innerHTML=str;
							return span;
						}, 
						function(design) {						
							 var span=document.createElement("span");
							 span.innerHTML=design.user.fullname;	
							 return span;
						},
						 
					    function(design) {
						   	 var span=document.createElement("span");
					    	 span.innerHTML="<input type='hidden' name='title' value='"+design.title +"'/>";
					    	 var td=document.createElement("td");	
							 td.innerHTML+=design.title;	
					    	 span.appendChild(td);					    		    	 
							 return span;
						},
						function(design) {
							var span=document.createElement("span"); 
							span.innerHTML=design.createTime;	
							return span;	
						},
						function(design) {			
								var span=document.createElement("span"); 
								span.innerHTML=design.viewCounts;	
								return span;	
						}
					 ];
					 

